/**
 * Created by wrl on 2017/9/7.
 */
var path = '../';
Vue.http.options.emulateJSON = true;
var vm;
$(function(){
vm=new Vue({
        el: '#form-role-au',
        data: {
            roleInfo:{},
            privilegeList:[],
            action:util.getUrlParams()['action'],
            updateId:util.getUrlParams()['updateId'],
            functionCodeMap:{}
        },
        mounted: function () {
            var self = this;

            //加载权限列表
            self.$http.get(path+'privilege/select',{}).then(function(res){
                self.$data.privilegeList = res.body.list;
            },function(res){
                console.log(res.body)
            })
            if(self.action == 'update' && self.updateId != null){
                self.$http.get(path+'role/updateInit/'+self.updateId,{}).then(function(response){
                    self.roleInfo = response.body;
                    var functionCodeList = response.body.functionCodeList;
                    for(var i =0;i<functionCodeList.length;i++){
                        self.functionCodeMap[functionCodeList[i]]=true;
                    }
                },function(res){
                    console.log(res.body)
                })
            }
        },
        filters:{
            funFilter:function(val){
                return val.substring(val.indexOf('-')+1,val.length)
            }
        },
        methods: {
            save:function(){
                var self = this;
                if(checkNotEmpty()){
                    var list = [];
                    $('.functionCodeList').each(function(index,one){
                        if($(one).prop('checked')){
                            list.push($(one).val())
                        }
                    });
                    self.$http.post(path+'role/save',{functionCodeList:list.toString(),roleName:self.roleInfo.roleName}).then(function(response){
                        if(response.body.flag == true){
                            tips('添加成功!',1);
                            setTimeout(function(){
                                parent.window.location.replace(parent.window.location.href);
                            },500);
                        }else{
                            if(response.body.sysErrorMsg)
                                tips(response.body.sysErrorMsg,5);
                            else if(response.body.failMsg)
                                tips(response.body.failMsg,5);
                            else
                                tips('未知错误',5);
                        }
                    },function(res){
                        console.log(res.body);
                    })
                }
            },
            update:function(){
                var self = this;
                if(checkNotEmpty()){
                    var list = [];
                    $('.functionCodeList').each(function(index,one){
                        if($(one).prop('checked')){
                            list.push($(one).val())
                        }
                    });
                    //var param = '?id='+self.updateId+'&functionCodeList='+list+'&roleName='+self.roleInfo.roleName
                    self.$http.post(path+'role/update',{id:self.updateId,functionCodeList:list.toString(),roleName:self.roleInfo.roleName}).then(function(response){
                        if(response.body.flag == true){
                            tips('更新成功!',1);
                            setTimeout(function(){
                                parent.window.location.replace(parent.window.location.href);
                            },500);
                        }else{
                            if(response.body.sysErrorMsg)
                                tips(response.body.sysErrorMsg,5);
                            else if(response.body.failMsg)
                                tips(response.body.failMsg,5);
                            else
                                tips('未知错误',5);
                        }
                    },function(res){
                        console.log(res.body);
                    })
                }
            },
            cb_nofun:function(e){
                $(e.target).closest("dl").find("dd input:checkbox").prop("checked", $(e.target).prop("checked"));
            },
            cb_fun:function(e) {
                var checkbox = e.target;
                var l = $(checkbox).parent().parent().find("input:checked").length;
                var l2 = $(checkbox).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
                if($(checkbox).prop("checked")) {
                    $(checkbox).closest("dl").find("dt input:checkbox").prop("checked", true);
                    $(checkbox).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", true);
                } else {
                    if(l == 0) {
                        $(checkbox).closest("dl").find("dt input:checkbox").prop("checked", false);
                    }
                    if(l2 == 0) {
                        $(checkbox).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked", false);
                    }
                }
            }
        }
    })

});



/**
 * 校验input是否为空
 * @returns {boolean}
 */
function checkNotEmpty(){
    var flag = true;
    $('.input-text:not(".noInput")').each(function(index,ele){
        if($(this).val() == ''){
            $(this).focus().blur();
            flag = false;
            return true;
        }
    });
    return flag;
}